/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2016年11月8日         yellowlgx
 */

package com.skyworth.ui.newrecycleview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class NewVerticalScrollBar extends FrameLayout
{
    private int mScrollBarHeight;

    private ImageView mScrollBarView;
    private LayoutParams mScrollBarParams;

    /**
     * @Fields lastPosition 记录上一次移动的位置
     */
    private int lastPosition;

    /**
     * @Fields mScrllBarFocusble 滚动条是否可落焦<br/>
     * true表示可获焦，false反之
     */
    private boolean mScrollBarFocusble = false;

    private boolean isNomalState = true;

    private OnScrollBarScrollListener mListener;

    public interface OnScrollBarScrollListener
    {
        public void onScrollBarScrollPosision(float position);
    }

    public NewVerticalScrollBar(Context context)
    {
        super(context);
        mScrollBarView = new ImageView(context);
        mScrollBarView.setFocusable(false);
        mScrollBarParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mScrollBarView.setLayoutParams(mScrollBarParams);
        this.addView(mScrollBarView);
    }

    public void resetScrollBar()
    {
        mScrollBarParams.topMargin = 0;
        mScrollBarView.setLayoutParams(mScrollBarParams);
    }

    public void setOnScrollBarScrollListener(OnScrollBarScrollListener listener)
    {
        mListener = listener;
    }

    public void setIsNomalState(boolean nomal)
    {
        isNomalState = nomal;
    }

    /**
     * @param mScrollBarFocusble 设置滚动条是否可获焦
     */
    public void setScrollBarFocusble(boolean mScrollBarFocusble)
    {
//        this.mScrollBarFocusble = false;
//        mScrollBarView.setFocusable(false);
        this.mScrollBarFocusble = mScrollBarFocusble;
        mScrollBarView.setFocusable(mScrollBarFocusble);
        mScrollBarView.setFocusableInTouchMode(mScrollBarFocusble);
    }

    /**
     * @return boolean
     * @Description 获取滚动条是否可获焦状态
     * @date 2016-1-6
     */
    public boolean getScrollBarFocusBle()
    {
        return mScrollBarFocusble;
    }

    /**
     * @return boolean
     * @Description 获取滚动条是否获焦状态
     * @date 2016-1-6
     */
    public boolean hasFocused()
    {
        return mScrollBarView.hasFocus();
    }

    /**
     * @param position 具体位置
     * @Description 设置滚动条具体滚动到的位置<br/>
     * 此位置是距离顶部的距离<br/>
     * @date 2016-1-6
     */
    public void setScrollBarPosition(int scrollY)
    {
//        if (isNomalState)
//            mScrollBarParams.topMargin += (position - lastPosition);
//        else
//        {
//            if (position > 0)
//                mScrollBarParams.topMargin += (Math.abs(position) - Math.abs(lastPosition));
//            else
//                mScrollBarParams.topMargin -= (Math.abs(position) - Math.abs(lastPosition));
//        }
//        if (mScrollBarParams.topMargin < 0)
//        {
//            mScrollBarParams.topMargin = 0;
//            isNomalState = true;
//        }
//        mScrollBarView.setLayoutParams(mScrollBarParams);
//        if (mListener != null)
//            mListener.onScrollBarScrollPosision(mScrollBarParams.topMargin);
//        lastPosition = position;

//        float y = 0;
//        if (isNomalState)
//            y = mScrollBarView.getY() + (position - lastPosition);
//        else
//        {
//            if (position > 0)
//                y = mScrollBarView.getY() + (Math.abs(position) - Math.abs(lastPosition));
//            else
//                y = mScrollBarView.getY() - (Math.abs(position) - Math.abs(lastPosition));
//        }
//        if (y < 0)
//        {
//            y = 0;
//            isNomalState = true;
//        }
        if (mScrollBarView.animate() != null)
            mScrollBarView.animate().cancel();
        mScrollBarView.animate().translationY(scrollY).setDuration(100).start();
        ViewPropertyAnimator anim = mScrollBarView.animate();

//        mScrollBarView.animate().setUpdateListener(new ValueAnimator.AnimatorUpdateListener()
//        {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation)
//            {
//                if (mListener != null)
//                    mListener.onScrollBarScrollPosision(mScrollBarView.getY());
//            }
//        });



//        ObjectAnimator anim = ObjectAnimator.ofFloat(mScrollBarView, "translationY", mScrollBarView.getY(),
//                scrollY);
//        anim.setDuration(200);
//        anim.start();
//        anim.setListener(new ValueAnimator.AnimatorUpdateListener()
//        {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation)
//            {
//                if (mListener != null)
//                    mListener.onScrollBarScrollPosision(mScrollBarView.getY());
//            }
//        });
//        mScrollBarView.setY(scrollY);
        lastPosition = scrollY;
    }


    public void setScrollBarToPosition(int position)
    {
//        mScrollBarParams.topMargin = position;
//        mScrollBarView.setLayoutParams(mScrollBarParams);
//        if (mListener != null)
//            mListener.onScrollBarScrollPosision(mScrollBarParams.topMargin);
        mScrollBarView.setY(position);
        if (mListener != null)
            mListener.onScrollBarScrollPosision(mScrollBarView.getY());
    }

    public void setFirstScrollBarPosition(int position)
    {
        float y = mScrollBarView.getY() + (position - lastPosition);
        if (y < 0)
        {
            y = 0;
        }
        mScrollBarView.setY(y);
        if (mListener != null)
            mListener.onScrollBarScrollPosision(mScrollBarView.getY());

//        mScrollBarParams.topMargin += (position - lastPosition);
//        if (mScrollBarParams.topMargin < 0)
//        {
//            mScrollBarParams.topMargin = 0;
//        }
//        mScrollBarView.setLayoutParams(mScrollBarParams);
    }

    /**
     * @return ImageView
     * @Description TODO获取滚动条View<br/>
     * @date 2016-1-6
     */
    public ImageView getScrollBarView()
    {
        return mScrollBarView;
    }

    /**
     * @return int
     * @Description 获取滚动条ScrollBar高度<br/>
     * @date 2016-1-6
     */
    public int getScrollBarViewHeight()
    {
        return mScrollBarHeight;
    }

    /**
     * @return int
     * @Description 获取滚动条总高度<br/>
     * @date 2016-1-6
     */
    public int getScrollBarHeight()
    {
        ViewGroup.LayoutParams params = this.getLayoutParams();
        if (params != null)
            return params.height;
        return 0;
    }

    /**
     * @param resid 资源ID
     * @Description 设置滚动条背景<br/>
     * @date 2016-1-6
     */
    public void setScrollBarBg(int resid)
    {
        this.setBackgroundResource(resid);
    }

    /**
     * @param res 资源ID
     * @Description 设置滚动条图标<br/>
     * @date 2016-1-6
     */
    public void setScrollBarIcon(int res)
    {
        BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(res);
        if (bd != null)
            mScrollBarHeight = bd.getBitmap().getHeight();
        mScrollBarView.setBackgroundResource(res);
    }

}
